package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    //公共实现继承至BaseServiceImpl

    //特有的实现自己单独写

    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    public void add(Department department) {
        departmentMapper.save(department);//这里新增以后我们返回了自增id
        this.update(department);
    }


    @Override
    public void update(Department department) {
        //处理部门所属路径字段
        String selfDirPath = "";
        if (department.getParent() == null){//如果是顶级部门
            selfDirPath = "/" + department.getId();
        }else {//如果不是顶级部门
            Department parent = departmentMapper.loadById(department.getParent().getId());//查出父部门
            selfDirPath = parent.getDirPath() + "/" + department.getId(); //父部门的dirPath + / + 自己的id
        }
        //把拼好的部门所属路径设置进去
        department.setDirPath(selfDirPath);
        //更新
        departmentMapper.update(department);
    }


    /*获取部门树*/
    /*嵌套循环  性能也不好*/
    /*@Override
    public List<Department> getTree() {
        //只有每一个非顶级部门找到自己的父亲 并把自己装到父部门的children里面去 就完成了部门树的查询
        //1.查询出所有部门
        List<Department> all = departmentMapper.loadAll();
        //声明好要返回的集合 装顶级部门
        List<Department> result = new ArrayList<>();
//        2.遍历所有
        for (Department department : all) {
            if (department.getParent_id() == null){//如果是顶级部门 就放在result中
                result.add(department);
            }else {// 3.如果不是顶级部门 那就找自己的父部门 把自己装到父部门的children集合中
                for (Department parent : all) {
                    if (department.getParent_id() == parent.getId()){//找到了父部门
                        List<Department> children = parent.getChildren();//把自己装到父部门的children集合中
                        children.add(department);
                        break;//找到父部门了 结束内存循环
                    }
                }
            }
        }

        return result;
    }*/

    /*使用Map方式来查询部门树*/
    @Override
    public List<Department> getTree() {
        //只有每一个非顶级部门找到自己的父亲 并把自己装到父部门的children里面去 就完成了部门树的查询

        //1.查询出所有部门
        List<Department> all = departmentMapper.loadAll();

        //把所有部门转成map key是部门id value就是这个部门对象
//        Map<Long, Department> map = all.stream().collect(Collectors.toMap(Department::getId, item -> item));
        HashMap<Long, Department> map = new HashMap<>();
        for (Department department : all) {
            map.put(department.getId(),department);
        }

        //声明好要返回的集合 装顶级部门
        List<Department> result = new ArrayList<>();
//        2.遍历所有
        for (Department department : all) {
            if (department.getParent_id() == null){//如果是顶级部门 就放在result中
                result.add(department);
            }else {// 3.如果不是顶级部门 那就找自己的父部门 把自己装到父部门的children集合中
                Long pid = department.getParent_id();
                Department parent = map.get(pid);
                if (parent != null){
                    List<Department> children = parent.getChildren();
                    children.add(department);
                }

            }
        }

        return result;
    }
}
